package com.cola.service;

import com.cola.dao.MessageDao;
import com.cola.entity.order.OrderMessage;
import com.cola.enums.OrderMessageCheckStatus;

import java.util.List;

/**
 * 消息Service
 * @author Max
 * @since 2017/9/18
 */
public class MessageService {
    private static volatile MessageService instance = null;
    public static MessageService getInstance() {
        if (instance == null) {
            synchronized (MessageService.class) {
                if (instance == null) {
                    instance = new MessageService();
                }
            }
        }
        return instance;
    }
    /**
     * 获取消息列表
     * @param mepNo 经销商编号;beginNo 起始位置;pageSize 页面大小
     * @return List<OrderMessage>
     * @author Max
     * @since 2017/9/18
     */
    public List<OrderMessage> getOrderMessages(String mepNo, Integer beginNo, Integer pageSize){
        return MessageDao.getInstance().getOrderMessages(mepNo,beginNo,pageSize);
    }


    /**
     * 添加一条新消息
     * @param orderNo 订单编号
     * @param mepNo 经销商编号
     * @param customerName 店铺名
     * @param createTime 订单创建时间
     * @return
     * @author Max
     * @since 2017/9/18
     */
    public boolean addOrderMessage(String orderNo,String mepNo,String customerName,String createTime){
        OrderMessage message = new OrderMessage();

        message.setOrder_no(orderNo);

        message.setMep_no(mepNo);

        message.setMessage_content("【"+customerName+"】下了一笔新订单。");

        message.setCreate_time(createTime);

        return MessageDao.getInstance().addOrderMessage(message);
    }


    /**
     * 阅读订单信息处理
     * @param messageId
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean readOrderMessage(Integer messageId){
        OrderMessage message = new OrderMessage();

        message.setMessage_id(messageId);

        message.setIs_read(1);

        return MessageDao.getInstance().updateOrderMessageIsRead(message);
    }


    /**
     * 信息接单处理
     * @param messageId
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean acceptOrderMessage(Integer messageId){
        OrderMessage message = new OrderMessage();

        message.setMessage_id(messageId);

        message.setIs_read(OrderMessageCheckStatus.ACCEPT.getValue());

        return MessageDao.getInstance().updateOrderMessageIsRead(message);
    }


    /**
     * 拒单信息处理
     * @param messageId
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/18
     */
    public boolean rejectOrderMessage(Integer messageId){
        OrderMessage message = new OrderMessage();

        message.setMessage_id(messageId);

        message.setIs_read(OrderMessageCheckStatus.REJECT.getValue());

        return MessageDao.getInstance().updateOrderMessageIsRead(message);
    }


}
